253B - Physics Practical - CodeForces Solution


binary search dp sortings two pointers *1400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
 	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
		
	ll n;
	cin >> n;
	
	vector<ll> v(n,0);
	for(ll i=0;i<n;i++)
        cin >> v[i];
	
	ll i=0,j=0,maxi=v[0],mini = v[0],ans=0;
	sort(v.begin(),v.end());
	
	while(j<n)
	{
		maxi = v[j];
		mini = v[i];
		
		while(i<=j && maxi>2*mini)
		{			i++;
			maxi = v[j];
			mini = v[i];
		}
		ans = max(ans,(ll)j-i+1);
		j++;
	}
	
	cout << n-ans << endl;
	return 0;
}


Comments

Submit
1 Comments
  • 11/4/2023 18:45 - Africa/Cairo

#include <bits/stdc++.h>
#define endl '\n'
#define Fast ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;


int main() {
    Fast;
    int n;cin>>n;
    vector<int>v(n);
    for (int i =0 ;i < n ;i++)
    {
        cin>>v[i];
    }
    sort (v.begin(),v.end());
    // binary search by searching on mid
    if (v[n-1]-v[0]<=2) cout<<0;
    else
    {
        int lo = 0, hi = v.size() - 1, mid;
        while (lo <= hi)
        {
             mid = (lo + hi) / 2;
             if (hi-v[0]<=2) break;
             if (v[mid]-v[0]>2) hi =mid-1;
             else if (v[mid]-v[0]<=2) lo =mid+1;


        }
        cout<<v.size()-(hi-1);
    }
        return 0;
}


More Questions

1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers